<?php 
set_time_limit(0);
ini_set("memory_limit", "12M");
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
//sort Carnumber
function CompareCarnumberASC($a, $b){
	if ($a[0] == $b[0]) {
		return 0;
	}
	return ($a[0] < $b[0]) ? -1 : 1;
}
function CompareCarnumberDESC($a, $b){
	if ($a[0] == $b[0]) {
		return 0;
	}
	return ($a[0] > $b[0]) ? -1 : 1;
}
// sort Code1
function CompareCode1ASC($a, $b){
	if ($a[1] == $b[1]) {
		return 0;
	}
	return ($a[1] < $b[1]) ? -1 : 1;
}
function CompareCode1DESC($a, $b){
	if ($a[1] == $b[1]) {
		return 0;
	}
	return ($a[1] > $b[1]) ? -1 : 1;
}
// sort Cartype
function CompareCartypeASC($a, $b){
	if ($a[2] == $b[2]) {
		return 0;
	}
	return ($a[2] < $b[2]) ? -1 : 1;
}
function CompareCartypeDESC($a, $b){
	if ($a[2] == $b[2]) {
		return 0;
	}
	return ($a[2] > $b[2]) ? -1 : 1;
}
//sort Code2
function CompareCode2ASC($a, $b){
	if ($a[3] == $b[3]) {
		return 0;
	}
	return ($a[3] < $b[3]) ? -1 : 1;
}
function CompareCode2DESC($a, $b){
	if ($a[3] == $b[3]) {
		return 0;
	}
	return ($a[3] > $b[3]) ? -1 : 1;
}

if (isset($_SESSION['s_alarm']) and is_array($_SESSION['s_alarm']) and isset($_REQUEST['flag'])) {
	
	$mylist = $_SESSION['s_alarm'];
    if($_REQUEST['flag']=='10'){
		usort($mylist, 'CompareCarnumberASC');
	}elseif($_REQUEST['flag']=='11'){
		usort($mylist, 'CompareCarnumberDESC');
	}
	elseif($_REQUEST['flag']=='20'){
		usort($mylist, 'CompareCode1ASC');
	}elseif($_REQUEST['flag']=='21'){
		usort($mylist, 'CompareCode1DESC');
	}
	elseif($_REQUEST['flag']=='30'){
		usort($mylist, 'CompareCartypeASC');
	}elseif($_REQUEST['flag']=='31'){
		usort($mylist, 'CompareCartypeDESC');
	}
	elseif($_REQUEST['flag']=='40'){
		usort($mylist, 'CompareCode2ASC');
	}elseif($_REQUEST['flag']=='41'){
		usort($mylist, 'CompareCode2DESC');
	}	
	$_SESSION['s_alarm'] = $mylist;
	
	$pageNum = $_REQUEST['pageNum'];
	$rows_per_page = $_REQUEST['rows_per_page'];
	$pageend = $pageNum*$rows_per_page;
	$pagestart = $pageend - $rows_per_page;
	$index = 0;
	$real = 0;
	foreach ($mylist as $key =>$row){
		$index+=1;
		
		//array_push($mylist, array($row[carnumber],$row[code1],$row[cartype],$row[code2]));
		if ($index<=$pagestart) {
			continue;
		}elseif ($index > $pageend){
			break;
		}
		else{$real++;
	?>
		 <tr><td  width="10%" align="center"><?=$index?></td>
		 	 <td  width="30%"><?=$row[0]?></td>
		 	 <td  width="20%"><?=$row[1]?></td>
		 	 <td  width="20%"><?=$row[2]?></td>
		 	 <td   class="last"><?=$row[3]?></td>
		 </tr> 
						 
	<?php  
		}
	}
	if ($real>0 and $real<17) {
		 	foreach(range(0,17-$real) as $i){
		 		?>
		 		<tr>
		 		<td width="10%">&nbsp;</td>
			 	<td width="30%">&nbsp;</td>
			 	<td width="20%">&nbsp;</td>
			 	<td width="20%">&nbsp;</td>
			 	<td   class="last">&nbsp;</td>
			 	</tr>
		 	<?php }
	}

}
?>  